/***************************************************************************
 * SDL_rwurl.c -- implimentation for URL parser joining with SDL_RWops     *
 * handlers                                                                *
 *                                                                         *
 *   Copyright (C) 2004 by Tyler Montbriand                                *
 *   tsm@accesscomm.ca                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_rwurl.h"
#include "SDL_rwhttp.h"
#include "SDL_rwsock.h"

int main(int argc, char *argv[])
{
  SDL_RWops *rw;

  if(argc>1)
  {
    int n;
    for(n=1; n<argc; n++)
    {
      rw=SDL_RWFromURL(argv[n]);
      if(rw!=NULL)
      {
        fprintf(stderr,"Succeeded\n");
        SDL_RWclose(rw);
      }
      else
        fprintf(stderr,"Failed\n");
    }
  }
  else
  {
    rw=SDL_RWFromURL("http://burningsmell.org");
    if(rw!=NULL)
    {
      fprintf(stderr,"Succeeded\n");
      SDL_RWclose(rw);
    }
    else
      fprintf(stderr,"Failed\n");
  }
  return(0);
}

inline static char *advance_buf(const char **ibuf, char **buf, int *urlpos)
{
  char *retval=*buf;
  (*ibuf)+=(*urlpos);
  (*buf)+=(*urlpos)+1;
  (*buf)[0]='\0';
  (*urlpos)=0;
  return(retval);
}

SDL_RWops *SDL_RWFromURL(const char *url)
{
  int urlpos=0;
  char buf[1024];
  char *protocol=NULL,*auth=NULL,*server=NULL,*port=NULL;
  char *end=buf;
  const char *orig_url=url;

  if(url==NULL)
  {
    SDL_SetError("%s:  NULL URL",__FUNCTION__);
    return(NULL);
  }

  sscanf(url,"%[^:/]://%n",end,&urlpos);
  if(urlpos==0)
  {
    SDL_SetError("%s:  Invalid URL %s",url);
    return(NULL);
  }

  protocol=advance_buf(&url,&end,&urlpos);

  fprintf(stderr,"Got protocol \"%s\"\n",protocol);
  fprintf(stderr,"Rest of string is \"%s\"\n",url);

  sscanf(url,"%[^@]@%n",end,&urlpos);
  if(urlpos!=0)
  {
    auth=advance_buf(&url,&end,&urlpos);
    fprintf(stderr,"auth(%s)\n",auth);
  }

  fprintf(stderr,"Remaining URL is \"%s\"\n",url);
  sscanf(url,"%[^/]%n",end,&urlpos);
  if(urlpos!=0)
  {
    server=advance_buf(&url,&end,&urlpos);
    fprintf(stderr,"Got server \"%s\"\n",server);
    port=strchr(server,':');
    if(port!=NULL)
    {
      port[0]='\0';
      port++;
      fprintf(stderr,"Got port %s\n",port);
    }
  }

  fprintf(stderr,"Remaining path is \"%s\"\n",url);

//  fprintf(stderr,"URL is %s://[%s:%s@][%s][:%s]%s\n",protocol,user,pass,server,port,url);

  // We've parsed the URL now.  Now to determine which backend to use...

  switch(protocol[0])
  {
  case 't':
    if(strcmp("elnet",protocol+1)==0)
    {
      if(port==NULL) port="23";
      fprintf(stderr,"Using SDL_RWsock backend for telnet://\n");
      return(SDL_RWFromHost(server,atoi(port)));
    }
    goto UNKNOWN_PROTOCOL;
    break;

  case 'f':
    if(strcmp("ile",protocol+1)==0)
    {
      fprintf(stderr,"Using SDL_RWFromFile backend for file:// URL\n");
      if(strlen(url)==0)
      {
        SDL_SetError("No filename given for file://");
        return(NULL);
      }

      return(SDL_RWFromFile(url,"rb"));
    }

    goto UNKNOWN_PROTOCOL;
    break;

  case 'h':
    if(strcmp("ttp",protocol+1)==0)
    {
      fprintf(stderr,"Using SDL_RwFromHTTP backend for http:// URL\n");
      if(port==NULL) port="80";
      if(strlen(url)==0) url="/";

      return(SDL_RWFromHTTP(NULL,server,url,atoi(port)));
    }
    goto UNKNOWN_PROTOCOL;

  UNKNOWN_PROTOCOL:
  default:
    SDL_SetError("Unhandled protocol \"%s\"",protocol);
    return(NULL);
    break;
  }
}
